package com.longcai.controller.customer;


import com.alibaba.fastjson.JSONObject;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.service.customer.ClyjUserSignService;
import com.longcai.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 用户签到表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-04-01
 */
@Controller
@RequestMapping("/clyjUserSign")
public class ClyjUserSignController {

    @Autowired
    private ClyjUserSignService clyjUserSignService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 用户签到
     *
     * @param params
     * @return
     */
    @RequestMapping("/getUserSign")
    @ResponseBody
    public ApiResult getUserSign(@RequestBody JSONObject params) {
        String day = params.getString("day");
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        return this.clyjUserSignService.getUserSign(clyjUser.getUserId(), day);
    }

    /**
     * 用户签到明细
     *
     * @param params
     * @return
     */
    @RequestMapping("/getSignList")
    @ResponseBody
    public ApiResult getSignList(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        return this.clyjUserSignService.getSignList(clyjUser.getUserId());
    }

    /**
     * 获取签到天数
     *
     * @param params
     * @return
     */
    @RequestMapping("/getSignDay")
    @ResponseBody
    public Map<String, Object> getSignDay(@RequestBody JSONObject params) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //创建日期的对象，通过format()方法将日期对象格式化，格式化后是String类型
        Date date = new Date();
        String date1 = dateFormat.format(date);
        //获取前天的日期
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        String date2 = dateFormat.format(calendar.getTime());
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            resultMap.put("code", "fail");
            resultMap.put("errorMsg", "您好，你的账号已登陆失效，请重新登陆！");
            return resultMap;
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            resultMap.put("code", "fail");
            resultMap.put("errorMsg", "您好，你的账号已登陆失效，请重新登陆！");
            return resultMap;
        }
        return this.clyjUserSignService.getSignDay(clyjUser.getUserId(), date1, date2);
    }
}